<?php

namespace App\Http\Controllers;

use App\Helper;
use App\Models\Advert;
use App\Models\AdvertClickLog;
use App\Models\AdvertPrice;
use App\Models\Character;
use App\Models\Pay as PayModel;
use App\Models\Player;
use App\Models\Server;
use App\Models\ServerAuth;
use App\Models\ServerEngine;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use SoapClient;
use SoapParam;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    public function index()
    {
        $date = Carbon::now()->toDateString();
        $adList=AdvertPrice::where('price','>',0)->where('date',$date)
            ->orderBy('price','desc')
            ->orderByRaw('RAND()')
            ->take(3)->get();

        $hotServers=Server::where('state','>',1)->where('endTime','>',Carbon::now())->orderBy('state','desc')->take(6)->get();

        $newServers=Server::where('state','>',0)->where('endTime','>',Carbon::now())->orderBy('created_at','desc')->take(12)->get();
        return view('welcome',compact('newServers','hotServers','adList'));
    }

    public function list()
    {
        $servers=Server::where('state','>',0)->orderBy('created_at','desc')->paginate(24);
        return view('list',compact('servers'));
    }

    public function Keira3(){
        return view('Keira3');
    }
    public function Keira3_download(){
        return response()->download( storage_path('app/public/Keira3 3.0.1.rar'));
    }
    public function Keira3_download_linux(){
        return response()->download( storage_path('app/public/Keira3 3.5.2中文版.exe'));
//        return response()->download( storage_path('app/public/Keira3 3.4.2.exe'));
    }


    public function install($flag='win'){
        return view('install_'.$flag);
    }

    public function edu(){
        return view('edu');
    }

    public function ads(){
        $date = Carbon::now()->toDateString();
        $adList=AdvertPrice::where('price','>',0)->where('date',$date)
            ->orderBy('price','desc')
            ->orderByRaw('RAND()')
            ->paginate(10);

        return view('ads',compact('adList'));
    }

    public function ad(Request $request,Advert $advert){
        AdvertClickLog::create([
            'advert_id'=>$advert->id,
            'ip'=>$_SERVER['REMOTE_ADDR']
        ]);
//        dd($advert);
        return redirect($advert->url);
    }



    public function recommend(Request $request){
        if (Auth::check())
            return "<h1>您已经登录，请将推荐链接发送给其他人注册</h1>";
        else{
            return redirect('/register?rec='.$request->get('rec',0));
        }
    }

    public function engineAuth(Request $request){
        $code = $_GET["code"];
        $guid = $_GET["guid"];
        $sign = $_GET["sign"];
        $t = $_GET["t"];

        $se=ServerEngine::where('code',$code)->first();
        if (!$se) return 'error:errEngine';
        $key=$se->key;

        if (md5($code . $guid . $key . $t) !== $sign) {
            return 'error:errSign';
            return 'error:errSign'.md5($code . $guid . $key . $t);
        }

        $sa=ServerAuth::where('guid',$guid)->where('server_engine_id',$se->id)->first();
        if (!$sa) return 'error:noAuth';

        $endTime=strtotime("+1 year");
        if ($sa->endTime){
            $endTime=strtotime($sa->endTime);
        }
        $t = time();
        if ($t>$endTime){
            return 'error:authExpiration';
        }
        return '|success|:'.md5($code . $key . $t) . $t . $endTime;
    }

    public function test(){
        $COMMAND="server motd";

        $soapInfo = [
            'soap_uri' => 'urn:AC',
//            'soap_uri' => 'urn:MaNGOS',
            'soap_host' => '127.0.0.1',
            'soap_port' => 7878,
            'soap_acc' => 'admin',
            'soap_pwd' => '53957105'
        ];

        try {
            $conn = new SoapClient(NULL, array(
                'location' => 'http://' . $soapInfo['soap_host'] . ':' . $soapInfo['soap_port'] . '/',
                'uri' => $soapInfo['soap_uri'],
                'style' => SOAP_RPC,
                'login' => $soapInfo['soap_acc'],
                'password' => $soapInfo['soap_pwd']
            ));

            $res = $conn->executeCommand(new SoapParam($COMMAND, 'command'));
            unset($conn);
            dd($res);
        } catch (Exception $e) {
            $err = "Have error on soap: " . $e->getMessage();

            if (strpos($e, 'There is no such command') !== false) {
                $err = 'There is no such command!';
            }
        }
    }
}





















